/* ----------------------------- Noble Steed Engine----------------------------*
Authors : Daniel Randle, Alex Bourne
Date Created : April 05 2013

File:
	nsselectionmodel.h

Description:
	This file contains the NSSelectionModel class which manages the current reference selection in
	the engine
*---------------------------------------------------------------------------*/

#ifndef NSSELECTIONMODEL_H
#define NSSELECTIONMODEL_H
#define SEL_BOX_VERTS 2

class NSObjectReference;
class NSMapArea;
class NSShader;
class NSCamera;

#include <vector>
#include <nsmath3d.h>
#include <nsobject.h>
#include <nsboundingbox.h>


class NSSelectionModel
{
public:
	typedef std::vector<NSObjectReference*>::iterator Iterator;

	NSSelectionModel(NSMapArea * map);
	~NSSelectionModel();

	void add(NSObjectReference* ref);

	void clear();
	void checkCollision();

	void del();
	void draw(const NSMatrix4Df & projCamMat);
	void drawSelectionLines(const NSMatrix4Df & projCamMat);
	void drawSelectionBox();

	NSVec3Df getCenter();
	NSObject::ObjectType getCenterObjectType();
	float getDepth(NSCamera * cam);
	NSVec3Df getOrientation();
	NSVec2Df getSelBoxStartPos();
	NSBoundingBox::Type getWhichSelBoundingBox() const;

	bool isEmpty();
	bool isIn(NSObjectReference * ref);
	bool isSelectionBoxActive();

	void moveXYFromMouse(const NSVec2Df & currentMousePos, const NSVec2Df & lastMousePos);
	void moveZFromMouse(const NSVec2Df & currentMousePos, const NSVec2Df & lastMousePos);

	bool remove(NSObjectReference * ref);
	void rotate( const NSVec3Df & amount);
	void rotate( float xRotate, float yRotate, float zRotate);

	void scale(const NSVec3Df & amount);
	void scale(float xScale, float yScale, float zScale);

	void setSelectionBoxActive(bool set,const NSVec2Df & startingPoint = NSVec2Df());
	void setSelBoxColor(const NSVec3Df & color);
	void setTileSelectionColor(const NSVec3Df & color);
	void setOBBColor(const NSVec3Df & color);
	void setAABBColor(const NSVec3Df & color);
	void setDefaultSelColors();
	void setWhichSelBoundingBox(NSBoundingBox::Type box);

	void toggleSelectedGridSnap();
	void snapAllToGrid();

	void translate(const NSVec3Df & amount);
	void translate(float xMove, float yMove, float zMove);

	void updateSelBox(const NSVec2Df & cMousePos);

	Iterator begin();
	Iterator end();

private:
	void _loadSelBox();

	bool selectionPlaceable;

	std::vector<NSObjectReference*> currentSelection;
	NSMapArea * mapArea;
	NSShader * selTileShader;
	NSShader * selBoxShader;
	NSShader * obbShader;
	NSShader * aabbShader;
	NSShader * selBoxShaderOutline;
	NSVec3Df orientation;

	NSVec3Df selBoxColor;
	NSVec3Df tileSelectionColor;
	NSVec3Df OBBColor;
	NSVec3Df AABBColor;
	float fillOpacity;
	float lineOpacity;

	NSBoundingBox::Type selBoundingBox;

	unsigned int selBoxBuffer;
	std::vector<NSVec3Df> selBoxVerts;

	bool selectionBoxActive;
	float lineWidth;
	NSVec2Df startingMousePos;
};


#endif